package com.linzelin.config;

import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * 自定义配置国际化解析器
 */
public class MyLocalResolver implements LocaleResolver {

    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        /**
         * 在使用这个方法之前先解析下请求
         */
        String language = httpServletRequest.getParameter("language");
        //获取请求中的语言参数
        Locale locale = Locale.getDefault();
        //这个就是默认的语言参数
        if(!StringUtils.isEmpty(language)){
            //如果请求的连接带了国际化的参数
            //因为一般的请求都是zh_CN这种格式的
            String[] split = language.split("_");
            // 国家地区
            locale = new Locale(split[0], split[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }
}
